package com.steam.xbcc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.steam.xbcc.constans.GrowScoreConstant;
import com.steam.xbcc.domain.UserBaseInfo;
import com.steam.xbcc.domain.UserGrowLog;
import com.steam.xbcc.dto.GrowScoreDto;
import com.steam.xbcc.mapper.UserBaseInfoMapper;
import com.steam.xbcc.service.IUserBaseInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.steam.xbcc.service.IUserGrowLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 会员基本信息 服务实现类
 * </p>
 *
 * @author wt
 * @since 2022-09-23
 */
@Service
public class UserBaseInfoServiceImpl extends ServiceImpl<UserBaseInfoMapper, UserBaseInfo> implements IUserBaseInfoService {

    @Autowired
    private IUserGrowLogService userGrowLogService;

    @Override
    public void growScoreFromRegister(Long referId) {
        // 添加成长值
        saveUserBaseInfo(referId, GrowScoreConstant.GROW_SCORE_REFER_REGISTER);
        // 添加到日志
        saveUserBaseInfoLog(referId, GrowScoreConstant.GROW_SCORE_FROM_REGISTER, GrowScoreConstant.GROW_SCORE_REFER_REGISTER);
    }

    @Override
    public void growScoreFromReferBuysCourses(GrowScoreDto dto) {
        Integer growScore = getGrowScore(dto, GrowScoreConstant.GROW_SCORE_RATIO_FROM_REFER_BUY_COURSE);
        saveUserBaseInfo(dto.getId(),growScore);
        saveUserBaseInfoLog(dto.getId(),GrowScoreConstant.GROW_SCORE_FROM_REFER,growScore);
    }

    @Override
    public void growScoreFromRecharge(GrowScoreDto dto) {
        Integer growScore = getGrowScore(dto, GrowScoreConstant.GROW_SCORE_RATIO_FROM_RECHARGE);
        saveUserBaseInfo(dto.getId(),growScore);
        saveUserBaseInfoLog(dto.getId(),GrowScoreConstant.GROW_SCORE_FROM_RECHARGE,growScore);
    }

    @Override
    public UserBaseInfo getGrowScoreById(Long id) {
        return selectOne(new EntityWrapper<UserBaseInfo>().eq("id", id));
    }

    private Integer getGrowScore(GrowScoreDto dto, Double growScoreRatioFromReferBuyCourse) {
        BigDecimal bigDecimal = dto.getMoney().multiply(BigDecimal.valueOf(growScoreRatioFromReferBuyCourse));
        return bigDecimal.setScale(GrowScoreConstant.GROW_SCORE_ROUNDING_METHOD, BigDecimal.ROUND_DOWN).intValue();
    }

    private void saveUserBaseInfoLog(Long referId, String growScoreFromRegister, Integer growScoreReferRegister) {
        UserGrowLog userGrowLog = new UserGrowLog();
        userGrowLog.setCreateTime(new Date().getTime());
        userGrowLog.setUserId(referId);
        userGrowLog.setFromReason(growScoreFromRegister);
        userGrowLog.setScore(growScoreReferRegister);
        userGrowLogService.insert(userGrowLog);
    }

    private void saveUserBaseInfo(Long referId, Integer growScoreReferRegister) {
        Wrapper<UserBaseInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("id", referId);
        UserBaseInfo userBaseInfo = selectOne(wrapper);
        // 避免脏数据
        if (userBaseInfo != null) {
            int growScore = userBaseInfo.getGrowScore() + growScoreReferRegister;
            userBaseInfo.setGrowScore(growScore);
            userBaseInfo.setLevel(getLevel(growScore));
            super.updateById(userBaseInfo);
        }
    }

    private Integer getLevel(int growScore) {
        if (growScore >= 0 && growScore<=99) {
            return GrowScoreConstant.LEVEL_BRONZE;
        } else if (growScore >= 100 && growScore<=299) {
            return GrowScoreConstant.LEVEL_SILVER;
        } else if (growScore >= 300 && growScore<=599) {
            return GrowScoreConstant.LEVEL_GOLD;
        } else if (growScore >= 600 && growScore<=999) {
            return GrowScoreConstant.LEVEL_PLATINUM;
        } else if (growScore >= 1000 && growScore<1499) {
            return GrowScoreConstant.LEVEL_DIAMONDS;
        } else if (growScore >= 1500 && growScore<=2499) {
            return GrowScoreConstant.LEVEL_MASTER;
        } else {
            return GrowScoreConstant.LEVEL_KING;
        }
    }
}
